<template>
  <div class="block capa">
    <el-container>
      <div class="el-main">
        <div class="analysis-box">
          <div class="analysis-query">
            <label class="label-text">本站能力利用率：</label>
            <label class="label-text">{{ susage }}%</label>
          </div>

          <div class="analysis-query label-text">道岔/股道/区段能力利用率：</div>
          <div class="analysis-query">
            <el-select v-model="queryData.stationName" placeholder="请选择站名" style="width: 140px" class="filter-item">
              <el-option v-for="(item, index) in stationNameOptions" :key="index" :label="item" :value="item" />
            </el-select>
            <el-select v-model="queryData.deviceType" placeholder="请选择设备类型" style="width: 140px" class="filter-item">
              <el-option v-for="(item, index) in deviceTypeList" :key="index" :label="item" :value="item" />
            </el-select>
            <el-input v-model.trim="queryData.deviceName" placeholder="请输入设备名称" style="width: 140px" class="filter-item" />
            <el-date-picker :clearable="false" v-model="value1" type="daterange" style="width: 250px" placeholder="选择开始时间" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="startChange" placement="bottom-start"></el-date-picker>
            <!-- <el-date-picker :clearable="false"  v-model="queryData.endTime" type="date" style="width: 140px" placeholder="选择结束时间" :picker-options="pickerOptions" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="endChange" placement="bottom-start"></el-date-picker> -->
            <el-button type="success" class="reset" @click="reset">重置</el-button>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">查询</el-button>
          </div>
          <el-table :key="tableKey" v-loading="listLoading" :data="deviceUsages" border fit height="600">
            <el-table-column label="序号" prop="id" align="center" width="80">
              <template slot-scope="scope">
                <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column label="站点名称" prop="stationName" align="center"></el-table-column>
            <el-table-column label="设备类型" prop="deviceType" align="center"></el-table-column>
            <el-table-column label="设备名称" prop="deviceName" align="center"></el-table-column>
            <el-table-column label="能力利用率" prop="usageValueDict" align="center"></el-table-column>
            <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
          </el-table>

          <el-pagination background layout="total, sizes, prev, pager, next" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="pageSize" :page-sizes="[10, 50, 100, 200, 500]" :total="total" :current-page="currentPage" align="center"></el-pagination>
        </div>
      </div>
    </el-container>
  </div>
</template>

<script>
import { getStationNames } from '@/api/dataQuery'
import { getCapabilityUtilizationRate, getCapabilityTable } from '@/api/statistics'
// import { getFormatDate, getPreviousDayFormatDate } from '@/utils/datetime'
// import { isStringEmpty } from '@/utils/validate'
import dayjs from 'dayjs'
import './index.scss'
export default {
  data() {
    return {
      queryData: {
        stationName: '',
        startTime: dayjs()
          // .subtract(1, 'd')
          .format('YYYY-MM-DD 00:00:00'),
        endTime: dayjs().format('YYYY-MM-DD 23:59:59'),
        deviceType: '',
        deviceName: ''
        // page: 1,
        // limit: 10
      },
      value1: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
      listLoading: false,
      tableKey: 0,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      stationNameOptions: [],
      deviceTypeList: ['股道', '道岔', '区间区段', '无岔区段', '其他'],
      tableHeight: 0,
      deviceUsages: [],
      susage: 0,
      waitSignalTime: 0
    }
  },
  created() {
    this.getAllStationNames()
  },
  mounted() {},
  computed: {
    pickerOptions() {
      let _this = this
      return {
        disabledDate(time) {
          if (_this.queryData.startTime) {
            return time.getTime() < new Date(_this.queryData.startTime).getTime()
          } else {
            return false
          }
        }
      }
    }
  },
  watch: {},
  methods: {
    getAllStationNames() {
      this.stationNameOptions = []
      getStationNames().then(res => {
        if (res.code === 0) {
          this.stationNameOptions = res.data
          this.queryData.stationName = res.data[0]

          let newObj = this.queryData
          this.getTableData(newObj)
          this.getTopCard(newObj)
        } else {
          this.listLoading = true
          this.$message('暂无数据')
        }
      })
    },
    startChange(val) {
      // this.queryData.startTime = val + ' 00:00:00'
      this.queryData.startTime = val[0] + ' 00:00:00'
      this.queryData.endTime = val[1] + ' 23:59:59'
    },
    endChange(val) {
      this.queryData.endTime = val + ' 23:59:59'
    },
    reset() {
      this.queryData.endTime = dayjs().format('YYYY-MM-DD 23:59:59')
      this.queryData.startTime = dayjs().format('YYYY-MM-DD 00:00:00')
      this.queryData.deviceName = ''
      this.queryData.deviceType = ''
      this.value1 = [dayjs().format('YYYY-MM-DD 00:00:00'), dayjs().format('YYYY-MM-DD 23:59:59')]
      this.currentPage = 1
      this.pageSize = 10
      this.getTopCard(this.queryData)
      this.getTableData(this.queryData)
    },
    getTopCard(newobj) {
      getCapabilityUtilizationRate(newobj).then(res => {
        if (res.code == 0) {
          this.susage = res.data.susage
        } else {
          this.susage = 0
        }
      })
    },
    getTableData(newObj) {
      this.listLoading = true
      let obj = {
        page: this.currentPage,
        size: this.pageSize,
        obj: newObj
      }
      getCapabilityTable(obj).then(res => {
        if (res.code == 0) {
          this.deviceUsages = res.data.records
          this.total = res.data.total
          this.listLoading = false
        } else {
          this.deviceUsages = []
          this.total = 0
          this.$message(res.msg)
          this.listLoading = false
        }
      })
    },
    search() {
      this.getTableData(this.queryData)
      this.getTopCard(this.queryData)
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getTableData(this.queryData)
    },
    handleSizeChange: function(size) {
      this.pageSize = size
      this.getTableData(this.queryData)
    }
  }
}
</script>

<style scoped lang="scss">
el-table {
  ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
    width: 10px; /* 滚动条宽度 */
    height: 10px; /* 滚动条高度 */
  }
  /* 定义滚动条轨道 内阴影+圆角 */
  ::v-deep .el-table__body-wrapper::-webkit-scrollbar-track {
    box-shadow: 0px 1px 3px #071e4a inset; /* 滚动条的背景区域的内阴影 */
    border-radius: 10px; /* 滚动条的背景区域的圆角 */
    background-color: #071e4a; /* 滚动条的背景颜色 */
  }
  /* 定义滑块 内阴影+圆角 */
  ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
    box-shadow: 0px 1px 3px #00a0e9 inset; /* 滚动条的内阴影 */
    border-radius: 10px; /* 滚动条的圆角 */
    background-color: #00a0e9; /* 滚动条的背景颜色 */
  }
}
</style>
